package com.cloud.base.thread.volatiles;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class VolatileTest extends Thread {
    /**
     * lock 锁 汇编的指令 强制修改值，立马刷新主内存中 另外线程立马可见刷新主内存数据
     */
    private static boolean FLAG = true;

    @Override
    public void run() {
        while (FLAG) {
            log.info("****");
//            System.out.println("****");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        TestA test1 = new TestA();
        test1.setId(1);
        test1.setName("zhang");
        TestA test2 = new TestA();
        test2.setId(1);
        test2.setName("zhang1");
        List<TestA> objects = new ArrayList<>();
        objects.add(test1);
        objects.add(test2);

        System.out.println(
                !objects.stream().map(o -> o.getName()).collect(Collectors.toSet()).contains("zhang2"));
    }

    static class TestA {
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}